//
//  EditFormController.m
//  MClient
//
//  Created by  史正烨 on 09-10-19.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "EditFormController.h"
#import "MClientAppDelegate.h"
#import "MServerDAO.h"
#import "MCFunction.h"
#import "MCUIToolkit.h"
#import "MCControl.h"
#import "DateTimeSelectView.h"
#import "LookupSelectView.h"
#import "MCSectionBar.h"
#import "MCSwitch.h"
#import "WithFormButtonsBar.h"
#import "MCImageView.h"

@interface EditFormController (Private)
- (Boolean) isFormVaild;
- (void) setDataBeforeCommit;
@end

@implementation EditFormController
@synthesize dataObjectName;
@synthesize layoutName;
@synthesize keyValue;
@synthesize initialValues;
@synthesize formData;
@synthesize formLayout;
@synthesize coordField;
@synthesize jingField;
@synthesize weiField;
@synthesize mapView3,_search2,_coordinate;
#pragma mark ---- UIViewController方法 ----
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];
    
    //yuxiaotong
    mapView3 = [[BMKMapView alloc]init];
    //    self._mapView3.delegate = self;
    _search2 = [[BMKSearch alloc]init];
    self._search2.delegate = self;
    
	[self leftBarButton:@"返回" withStyle:UIBarButtonItemStylePlain selector:@selector(selectCancelAction:)];
	
	
	formLayoutCopy = [[MCUIToolkit getObjectCopy:formLayout] retain];
	
	self.title = [formLayoutCopy objectForKey:@"lbl"];
		
	self.addHelpButton = YES;
	self.helpUrl = @"/iphone/help/chapter05-01.html";
	// 图片下载并加载完成，返回图片尺寸信息
	imageSizeMap = [[NSMutableDictionary alloc] init];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(getImageSize:)
												 name:@"ImageSize" object:nil];
	self.isTabStyle = NO;
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]&&[[formLayoutCopy objectForKey:@"stn"] count] > 1) {
		self.isTabStyle = YES;
	}
	//根据扩展属性，设置uitableview的分割线
	self.separator = ![[[formLayoutCopy objectForKey:@"csty"] objectForKey:@"noseparatorline"] intValue];
	
	if (self.isTabStyle) {
		self.tableview.frame = CGRectMake(0, nToolBarHeight, self.width, self.height-nToolBarHeight);
		MCSectionBar *sectionBar = [[MCSectionBar alloc] initWithFrame:CGRectMake(0, 0, self.width, nToolBarHeight)];
		sectionBar.autoresizingMask = nMCViewAutoresizingMask;
		sectionBar.tag = -321;
		[sectionBar addSectionBarItems:[formLayoutCopy objectForKey:@"stn"] delegate:self];
		[self.clientView addSubview:sectionBar];
		[sectionBar release];
	}
	buttonCol = 1;
	if ([formLayoutCopy objectForKey:@"csty"]) {
		if ([[formLayoutCopy objectForKey:@"csty"] objectForKey:@"button_col"]) {
			buttonCol =  [[[formLayoutCopy objectForKey:@"csty"] objectForKey:@"button_col"] intValue];
			if (buttonCol > 3) {
				buttonCol = 3;
			}
		}
	}
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self didRotateFromInterfaceOrientation:0];
    NSLog(@"%d",[self.mapView3 retainCount]);
    
    [mapView3 viewWillAppear];
    self.mapView3.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    self.mapView3.showsUserLocation = YES;
}

-(void)viewWillDisappear:(BOOL)animated {
    [self.mapView3 viewWillDisappear];
     self.mapView3.delegate = nil; // 不用时，置nil
}
- (void)keyboardDidHidden:(NSNotification*)aNotification
{
	if (self.isTabStyle) {
		self.tableview.frame = CGRectMake(0, nToolBarHeight, self.width, self.height-nToolBarHeight);
	} else {
		self.tableview.frame = CGRectMake(0, 0, self.width, self.height);
	}
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
	[self keyboardDidHidden:nil];
	int withFormButtonsBarHeight = [MCUIToolkit getWithFormButtonsBarHeight:withFormButtons buttonCol:buttonCol];
	if (withFormButtonsBarHeight != 0) {
		WithFormButtonsBar *withFormButtonsBar = [[WithFormButtonsBar alloc] initWithFrame:CGRectMake(0, 0, self.width, withFormButtonsBarHeight)];
		withFormButtonsBar.tag = -8888;
		[withFormButtonsBar addWithFormButtons:withFormButtons buttonCol:buttonCol delegate:self];
		self.tableview.tableFooterView = withFormButtonsBar;
		[withFormButtonsBar release];
	}
	if (self.isTabStyle) {
		MCSectionBar *sectionBar = (MCSectionBar *)[self.clientView viewWithTag:-321];
		if (nil == sectionBar) {
			sectionBar = [[MCSectionBar alloc] initWithFrame:CGRectMake(0, 0, self.width, nToolBarHeight)];
			sectionBar.autoresizingMask = nMCViewAutoresizingMask;
			sectionBar.tag = - 321;
			[sectionBar addSectionBarItems:[formLayoutCopy objectForKey:@"stn"] delegate:self];
			[self.clientView addSubview:sectionBar];
			[sectionBar release];	
		}else {
			[sectionBar refreshFrame:CGRectMake(0, 0, self.width, nToolBarHeight) itemsCount:[[formLayoutCopy objectForKey:@"stn"] count]];
		}
	}
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self 
													name:@"ImageSize" 
												  object:nil];
    
    if (mapView3) {
        [mapView3 release];
        mapView3 = nil;
    }
    
	[dataObjectName release];
	[layoutName release];
	[keyValue release];
	[initialValues release];
	[formData release];
	[formLayout release];
	[editButtons release];
	[imageSizeMap release];
	[withFormButtons release];
	[formLayoutCopy release];
    [super dealloc];
}
#pragma mark ---- 数据加载方法 ----
- (void)getDataFromServer {
	self.operateFlag = @"getFormData";
	[[MServerDAO sharedInstance] getFormDataAsync:self.dataObjectName
									   layoutName:self.layoutName 
											  key:self.keyValue
									   isEditable:@"1"
										 delegate:self];
}

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	if ([self.operateFlag isEqualToString:@"getFormData"]) {
		self.formData = returnValue;
		
		NSMutableDictionary *datas = [self.formData objectForKey:@"data"];
		if (datas == nil){ 
			datas = [NSMutableDictionary dictionary];
			[self.formData setObject:datas forKey:@"data"];
		}
		NSMutableArray *sections = [formLayoutCopy objectForKey:@"stn"];
		if (sections != nil) {
			for(int i = 0; i < [sections count]; i++){
				NSMutableDictionary *section = [sections objectAtIndex:i];
				NSMutableArray *appItems = [section objectForKey:@"item"];
				for(int j = 0; j < [appItems count]; j++){
					NSMutableDictionary *appItem = [appItems objectAtIndex:j];
					
					NSString *name = [MCUIToolkit getFieldName:appItem];			
					NSString *fieldType = [MCUIToolkit getFieldType:appItem];
					
					NSMutableDictionary *fieldData = [datas objectForKey:name];
					if (fieldData == nil) {
						fieldData = [NSMutableDictionary dictionary];
						[fieldData setObject:@"" forKey:@"value"];
						[datas setObject:fieldData forKey:name];
					}
					
					if (![fieldData objectForKey:@"lbl"] || [@"" isEqualToString:[fieldData objectForKey:@"lbl"]]) {
						if([fieldType isEqualToString: @"3"] || [fieldType isEqualToString: @"4"] || [fieldType isEqualToString: @"U"]){
							NSString *relationObject = [MCUIToolkit getRelationObject:appItem];
							if(relationObject != nil && ![relationObject isEqualToString:@""] 
							   && nil != [fieldData objectForKey:@"value"] && ![@"" isEqualToString:[fieldData objectForKey:@"value"]]){
								id relReturnObject = [[MServerDAO sharedInstance] getFormData:relationObject layoutName:@"" key:[fieldData objectForKey:@"value"] isEditable:@"1"];
								if(relReturnObject){
									NSMutableDictionary *relObject = (NSMutableDictionary *)relReturnObject;
									if ([relObject objectForKey:@"desc"]) 
										[fieldData setObject:[relObject objectForKey:@"desc"] forKey:@"lbl"];
								}
							}
						}
					}
					
					// 置表单数据的强制赋值
					for (int l = 0; l < [self.initialValues count]; l++) {
						NSMutableDictionary *initialValue = [self.initialValues objectAtIndex:l];
						if ([[initialValue objectForKey:@"fld"] isEqualToString:name]) {
							[initialValue setObject:@"init"forKey:@"flag"];
							if([initialValue objectForKey:@"value"] == nil){
								break;
							}
							[fieldData setObject:[initialValue objectForKey: @"value"] forKey:@"value"];
							[datas setObject:fieldData forKey:name];
							if([fieldType isEqualToString: @"3"] || [fieldType isEqualToString: @"4"] || [fieldType isEqualToString: @"U"]){
								NSString *relationObject = [MCUIToolkit getRelationObject:appItem];
								NSMutableArray *lookupBackFills = [appItem objectForKey: @"lpbf"];
								if(relationObject != nil && ![relationObject isEqualToString:@""] 
								   && nil != [fieldData objectForKey:@"value"] && ![@"" isEqualToString:[fieldData objectForKey:@"value"]]){
									if (![fieldData objectForKey:@"lbl"] || lookupBackFills != nil) {
										id relReturnObject = [[MServerDAO sharedInstance] getFormData:relationObject layoutName:@"" key:[fieldData objectForKey:@"value"] isEditable:@"1"];
										if(relReturnObject){
											NSMutableDictionary *relObject = (NSMutableDictionary *)relReturnObject;
											if (![fieldData objectForKey:@"lbl"])
												[fieldData setObject:[relObject objectForKey:@"desc"] == nil?@"":[relObject objectForKey:@"desc"] forKey:@"lbl"];
											NSMutableDictionary *relDatas = [NSMutableDictionary dictionary];
											if ([relObject objectForKey:@"data"]){ 
												relDatas = [relObject objectForKey:@"data"];
											}
											
											if (lookupBackFills != nil) {
												for(int k = 0; k<[lookupBackFills count]; k++){
													NSMutableDictionary *lookupBackFill = [lookupBackFills objectAtIndex:k];
													NSString *fieldName = [lookupBackFill objectForKey:@"bf"];
													NSMutableDictionary *fieldDataLookupBack = [relDatas objectForKey:fieldName];
													[self setLookupBackFillByFieldName: [lookupBackFill objectForKey:@"fld"] fieldDataLookupBack:fieldDataLookupBack];	
												}
											}
										}
									}
								}
							}			
							break;
						}
					}	
					if ([fieldData objectForKey:@"v"]) {
						if ([[fieldData objectForKey:@"v"] boolValue] == NO) {
							[appItems removeObjectAtIndex:j];
							j--;
							continue;
						}
					}
					if ([fieldData objectForKey:@"r"]) {
						if ([[fieldData objectForKey:@"r"] boolValue] == YES) {
							[appItem setObject:[fieldData objectForKey:@"r"] forKey:@"rdy"];
						}
					}
					if ([fieldData objectForKey:@"m"]) {
						if ([[fieldData objectForKey:@"m"] boolValue] == YES) {
							if (nil == [fieldData objectForKey:@"r"] || [[fieldData objectForKey:@"r"] boolValue] == NO) {
								[appItem setObject:[fieldData objectForKey:@"m"] forKey:@"rqd"];
							}
						}
					}
				}
			}
		}	
		for(int m = 0; m < [self.initialValues count]; m++){
			NSMutableDictionary *initialValue = [initialValues objectAtIndex:m];
			if (![initialValue objectForKey:@"flag"]) {
				NSMutableDictionary *initialData = [NSMutableDictionary dictionary];
				[initialData setObject:[initialValue objectForKey:@"value"] == nil?@"":[initialValue objectForKey:@"value"] forKey:@"value"];
				[datas setObject:initialData forKey:[initialValue objectForKey:@"fld"]];
			}
		}
		
		NSMutableArray *cfgButtons = [formLayoutCopy objectForKey:@"btn"];
		NSMutableArray *dataButtons = [formData objectForKey:@"btn"];
		if (editButtons) {
			[editButtons release]; editButtons = nil;
		}
		editButtons = [[MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons] retain];
		withFormButtons = [MCUIToolkit getWithFormButtons:editButtons];
		int withFormButtonsBarHeight = [MCUIToolkit getWithFormButtonsBarHeight:withFormButtons buttonCol:buttonCol];
		if (withFormButtonsBarHeight != 0) {
			WithFormButtonsBar *withFormButtonsBar = [[WithFormButtonsBar alloc] initWithFrame:CGRectMake(0, 0, self.width, withFormButtonsBarHeight)];
			withFormButtonsBar.tag = -8888;
			[withFormButtonsBar addWithFormButtons:withFormButtons buttonCol:buttonCol delegate:self];
			self.tableview.tableFooterView = withFormButtonsBar;
			[withFormButtonsBar release];
		}
		
		[self rightBarButton:@"操作" withStyle:UIBarButtonItemStyleDone selector:@selector(selectAction:)];
		if ([editButtons count] == 1) {
			self.rightBarButtonItemTitle = [[editButtons objectAtIndex:0] objectForKey:@"lbl"];
		}
		else if ([editButtons count] > 1) {
			self.rightBarButtonItemTitle = @"操作";
		} else {
			self.rightBarButtonItem = nil;
		}
		
		//动态表单标签
		if ([returnValue objectForKey:@"csty"]) {
			NSMutableDictionary *csty = [returnValue objectForKey:@"csty"];
			if ([csty objectForKey:@"label"]) {
				self.title = [csty objectForKey:@"label"];
			}
		}
		
		[self reloadData];
		
	} else if ([self.operateFlag isEqualToString:@"getUploadFileName"]) {
		[self getUploadFileNameSuccess:returnValue];
	} else if ([self.operateFlag isEqualToString:@"uploadFile"]) {
		[self uploadFileSuccess];
	}
	if ([self.operateFlag isEqualToString:@"downloadFile"]) {
		[self downloadFileSuccess];
	}
}


#pragma mark ---- UITableView方法 ----

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]) {
		return 1;
	}
	return [[formLayoutCopy objectForKey:@"stn"] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]) {
		return [[[[formLayoutCopy objectForKey:@"stn"] objectAtIndex:tabIndex] objectForKey:@"item"] count];
	}
	return [[[[formLayoutCopy objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"item"] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]) {
		return 0;
	}
	if ([[formLayoutCopy objectForKey:@"stn"] count] <= 1) {
		return 0;
	}
	if ([[[formLayoutCopy objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"lbl"] == nil ||
		[@"" isEqualToString:[[[formLayoutCopy objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"lbl"]]) {
		return 0;
	}
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	int sectionIndex = indexPath.section;
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]) {
		sectionIndex = tabIndex;
	}
	NSMutableDictionary *appItem = [[[[formLayoutCopy objectForKey:@"stn"] 
									  objectAtIndex:sectionIndex]
									 objectForKey:@"item"]
									objectAtIndex:indexPath.row];
	int  lableStyle = [[appItem objectForKey:@"lbls"] intValue];
	NSString* aTitle = [MCUIToolkit getFieldLabel:appItem];
	NSString* aValue = [MCUIToolkit getFieldTextValue:self.formData
											  appItem:appItem
										 selectOption:[self.formData objectForKey:@"so"]];
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	/*
	if (cFieldType == '0') {
		return CGRectGetHeight(clientView.frame)/3 + 10;
	}
	 */
	if (cFieldType == 'S' && [[appItem objectForKey:@"rdy"] boolValue]) {
		NSString *name = [MCUIToolkit getFieldName:appItem];
		NSMutableDictionary *fieldData = [[formData objectForKey:@"data"] objectForKey:name];
		NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
		if (fileInfos == nil || [fileInfos count] < 1) 
			return [MCFunction cellHeight:lableStyle withTitle:aTitle withValue:aValue withAppItem:appItem];
		NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
		NSString *fileName = [[NSString stringWithFormat:@"indexPath_Seciton%d_Row%d_%@", indexPath.section,
							   indexPath.row, [fileInfo objectForKey:@"fname"]] retain];
		MCSize *size = [imageSizeMap objectForKey:fileName];
		if (size && size.height > 0) {
			if (lableStyle > 0) {
				if (size.width < 300.0) {
					return size.height+20;
				}else {
					return size.height/(size.width/300.0)+20;
				}
			}else {
				if (size.width < 200.0) {
					return size.height+20;
				}else {
					return size.height/(size.width/200.0)+20;
				}
			}
		}
	} else if (cFieldType == '0') {
		return CGRectGetHeight(clientView.frame) / 3 + 10;
	}	
	return [MCFunction cellHeight:lableStyle withTitle:aTitle withValue:aValue withAppItem:appItem];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section {

	if ([[formLayoutCopy objectForKey:@"stn"] count] > 1) {
		UILabel *sectionHead = [[[UILabel alloc] init] autorelease];
		
		sectionHead.text = [[[formLayoutCopy objectForKey:@"stn"] objectAtIndex:section] objectForKey:@"lbl"];
		sectionHead.textAlignment = UITextAlignmentCenter;
		sectionHead.font = [UIFont boldSystemFontOfSize:20];
		sectionHead.textColor = [UIColor whiteColor];
		sectionHead.backgroundColor = [UIColor colorWithPatternImage:self.userDefaults.skin_bgSectionImg];
		
		return sectionHead;
	} else {
		return nil;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	int sectionIndex = indexPath.section;
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]) {
		sectionIndex = tabIndex;
	}
	
	NSMutableDictionary *appItem = [[[[formLayoutCopy objectForKey:@"stn"] 
									  objectAtIndex:sectionIndex]
									 objectForKey:@"item"]
									objectAtIndex:indexPath.row];
	NSString *valueText = [MCUIToolkit getFieldTextValue:self.formData
												 appItem:appItem
											selectOption:[self.formData objectForKey:@"so"]];
	
	NSString *tipHolder = [[appItem objectForKey:@"style"] objectForKey:@"tipholder"];
	
	UITableViewCell *cell = [MCFunction createCellForEditForm:tableView 
													  appItem:appItem
													indexPath:indexPath
													 formView:self.clientView];
	UILabel *labelField = (UILabel *)[cell.contentView viewWithTag:-1];
	labelField.text = [MCUIToolkit getFieldLabel:appItem];
	
//	NSLog(@"---title=%@, value=%@---", labelField.text, valueText);
//	NSLog(@"-self.formData=%@----,appItem=%@", self.formData, appItem);
	
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);
	if (cFieldType != 'B') {
		if (cFieldType == '0') {
			@try {
				UIWebView *webView = (UIWebView *)[cell.contentView viewWithTag:-2];
				if (nil != valueText && ![@"" isEqualToString:valueText]) {
					[webView loadHTMLString:valueText baseURL:nil];
				}
				return cell;
			}
			@catch (NSException * e) {
			}
		}
		if (cFieldType == 'S' && [[appItem objectForKey:@"rdy"] boolValue]) {
			NSString *name = [MCUIToolkit getFieldName:appItem];
			NSMutableDictionary *fieldData = [[formData objectForKey:@"data"] objectForKey:name];
			NSString *df= [fieldData objectForKey:@"df"] == nil?@"":[fieldData objectForKey:@"df"];
			NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
			if (fileInfos == nil || [fileInfos count] < 1) 
				return cell;
			NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
			NSString *fileName = [fileInfo objectForKey:@"fname"];
			if (fileName == nil || [fileName length] == 0)
				return cell;
			NSString *fileLocation = [fileInfo objectForKey:@"lcn"];
			
			NSMutableDictionary *fileAttribute = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
												  fileName,@"fileName",
												  fileLocation,@"fileLocation",
												  @"download",@"actionType",
												  df,@"dataFrom",
												  [NSString stringWithFormat:@"%d", indexPath.section], @"indexPath_section",
												  [NSString stringWithFormat:@"%d", indexPath.row], @"indexPath_row",
												  nil];
			MCImageView *valueField = (MCImageView *)[cell.contentView viewWithTag:-2];
			
			NSMutableDictionary *field = [MCUIToolkit getField:appItem];
			if([field objectForKey:@"imgw"] && [field objectForKey:@"imgh"]) {
				valueField.imgw = [[field objectForKey:@"imgw"] intValue];;
				valueField.imgh = [[field objectForKey:@"imgh"] intValue];
			}
			
			if (valueField.image == nil) {
				valueField.fileAttribute = fileAttribute;
				[fileAttribute release];
			}else {
				int lableStyle = [[appItem objectForKey:@"lbls"] intValue];
				if (lableStyle > 0) {
					if (valueField.image.size.width < 300) {
						valueField.frame = CGRectMake(10, lableStyle==1?40:10, valueField.image.size.width, 35);
					}
				}else {
					if (valueField.image.size.width < 200) {
						valueField.frame = CGRectMake(110, lableStyle==1?40:10, valueField.image.size.width, 35);
					}
				}
			}
			return cell;
		}
		MCControl* valueField = (MCControl *)[cell.contentView viewWithTag:-2];
		if (tipHolder && valueField.style == MCControl_TextField) {
			((UITextField*)valueField.control).placeholder = tipHolder;
		}
		NSMutableDictionary *style = [appItem objectForKey:@"style"];
		if (style != nil) {
			float fontSize = 16.0;
			if ([style objectForKey:@"font_color"]) {
				valueField.textColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"font_color"]]];
			}
			if ([style objectForKey:@"font_size"]) {
				fontSize = [[style objectForKey:@"font_size"] floatValue]/0.75;
				valueField.font = [UIFont boldSystemFontOfSize:fontSize];
			}
			if ([style objectForKey:@"font_style"]) {
				NSString *fontStyle = [style objectForKey:@"font_style"];
				if ([fontStyle isEqualToString:@"B"]) {
					valueField.font = [UIFont boldSystemFontOfSize:fontSize];
				}else if ([fontStyle isEqualToString:@"I"]) {
					valueField.font = [UIFont italicSystemFontOfSize:fontSize];
				}else {
					valueField.font = [UIFont boldSystemFontOfSize:fontSize];
				}
			}
			if ([style objectForKey:@"font_align"]) {
				NSString *fontAlign = [style objectForKey:@"font_align"];
				if ([fontAlign isEqualToString:@"L"]) {
					valueField.textAlignment = UITextAlignmentLeft;
				}else if ([fontAlign isEqualToString:@"R"]) {
					valueField.textAlignment = UITextAlignmentRight;
				}else if ([fontAlign isEqualToString:@"M"]) {
					valueField.textAlignment = UITextAlignmentCenter;
				}
			}
		}
		if (cFieldType == 'H'||cFieldType == 'I'||cFieldType == 'K'||cFieldType == 'L') {
			NSArray *selectIcons = [valueText componentsSeparatedByString:@"::"];
			if ([selectIcons count] > 1) {
				for(UIImageView *imageView in [valueField subviews]){
					[imageView removeFromSuperview];
				}
				NSString *iconLabel = [selectIcons objectAtIndex:1];
				NSArray *icons = [iconLabel componentsSeparatedByString:@","];
				for (int j = 0; j < [icons count]; j++) {
					NSString *icon = [icons objectAtIndex:j];
					UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(32 * j, 0, 24, 24)];
					imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:icon] width:24.0 height:24.0];
					imageView.backgroundColor = [UIColor clearColor];
					[valueField addSubview:imageView];
					[imageView release];
				}
				return cell;
			} 
		} else if (cFieldType == 'N') {
			UIWebView *checkImage = (UIWebView *)[cell.contentView viewWithTag:-3];
			NSString *checkImageUrl = [NSString stringWithFormat:@"%@%@%@%@%@", self.userDefaults.current_ServerUrl, @"/beapp/checkcode?d=",dataObjectName,@"&f=",[MCUIToolkit getFieldName:appItem]];
			checkImage.scalesPageToFit = NO;
			[checkImage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:checkImageUrl]]];
		} 
		if (valueField.style == MCControl_TextField) {
			[(UITextField*)valueField.control addTarget:self 
												 action:@selector(textFieldDone:) 
									   forControlEvents:UIControlEventEditingDidEndOnExit];
			((UITextField*)valueField.control).delegate = self;
		}
		valueField.text = valueText;
		
	} else {
		MCSwitch *valueField = (MCSwitch *)[cell.contentView viewWithTag:-2];
		[valueField addTarget:self action:@selector(switchValue:) forControlEvents:UIControlEventValueChanged];
		if ([valueText isEqualToString:@"☑"]) {
			valueField.on = YES;
		}else {
			valueField.on = NO;
		}
	}

	return cell;
}	

- (void)switchValue:(UIControl *)sender {
	MCSwitch *valueField = (MCSwitch *)sender;
	NSMutableDictionary *fieldData = [[self.formData objectForKey:@"data"] objectForKey:valueField.fieldName];
	[fieldData setObject:valueField.on?@"1":@"0" forKey:@"value"];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	int sectionIndex = indexPath.section;
	if ([@"1" isEqualToString:[formLayoutCopy objectForKey:@"style"]]) {
		sectionIndex = tabIndex;
	}
	NSMutableDictionary *appItem = [[[[formLayoutCopy objectForKey:@"stn"] 
									  objectAtIndex:sectionIndex]
									 objectForKey:@"item"]
									objectAtIndex:indexPath.row];
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	NSString *fieldType = [MCUIToolkit getFieldType:appItem];
	char cFieldType;
	strncpy(&cFieldType, [fieldType UTF8String], 1);

	if (cFieldType != 'C' && cFieldType != 'D') {
		if ([DateTimeSelectView instance].isShown) {
			[[DateTimeSelectView instance] selectCancelAction];
		}
	}
	if (cFieldType != '3' && cFieldType != '4') {
		if ([LookupSelectView instance].isShown) {
			[[LookupSelectView instance] selectCancelAction];
		}
	}else {
		if (![@"3" isEqualToString:[appItem objectForKey:@"lpst"]]) {
			if ([LookupSelectView instance].isShown) {
				[[LookupSelectView instance] selectCancelAction];
			}
		}
	}

	[[self findFirstResponder] resignFirstResponder];
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	if (cFieldType == 'S' || cFieldType == 'J') {
		NSString *fieldName = [MCUIToolkit getFieldName:appItem];
		NSMutableDictionary *fieldData = [[self.formData objectForKey:@"data"] objectForKey:fieldName];
		self.dataFrom = [fieldData objectForKey:@"df"] == nil?@"":[fieldData objectForKey:@"df"];
		NSMutableArray *fileArray = [fieldData objectForKey:@"fi"];
		if (fileArray == nil) {
			fileArray = [NSMutableArray array];
			[fileArray addObject:[NSMutableDictionary dictionary]];
			[fieldData setObject:fileArray forKey:@"fi"];
		}
		if ([fileArray count] == 0) {
			[fileArray addObject:[NSMutableDictionary dictionary]];
		}
		self.fileAttach = [fileArray objectAtIndex:0];
		char cReadFlag = [cell.reuseIdentifier characterAtIndex:1];
		if (cReadFlag == 'T') {
			[self viewFileContent];
			return;
		}
		self.fileField = ((MCControl*)[cell.contentView viewWithTag:-2]).text;

		
		[self showActionSheetOfFile:[MCUIToolkit getField:appItem]];
		return;
	}
	
	[MCFunction selectDetailForEditFormByAppItem:self 
										withCell:cell
										 appItem:appItem
										  record:self.formData
									 isQueryForm:NO];
}

- (void)withFormButtonsAction:(id) sender {
	[[self findFirstResponder] resignFirstResponder];
	if ([DateTimeSelectView instance].isShown) {
		[[DateTimeSelectView instance] selectCancelAction];
	}
	if ([LookupSelectView instance].isShown) {
		[[LookupSelectView instance] selectCancelAction];
	}
	NSMutableDictionary *button = [withFormButtons objectAtIndex:((UIButton *)sender).tag];
	if ([MCErrorManagement showAuthMessage:button]) {
		return;
	}
	bool pageCheck = NO;
	if ([button objectForKey:@"pck"] != nil) {
		pageCheck = [[button objectForKey:@"pck"] boolValue];
	}
	[self setDataBeforeCommit];
	NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
	if (pageCheck) {
		if ([self isFormVaild]) {
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"edit_form",@"buttonType",
											   self.dataObjectName,@"dataObjectName",
											   self.formData,@"formData", nil];
			
			if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:[button objectForKey:@"cfmi"] 
						   naviController:self.navigationController];
			}else{
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:nil 
						   naviController:self.navigationController];
			}
			[parameters release];
		}
	}else {
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"edit_form",@"buttonType",
										   self.dataObjectName,@"dataObjectName",
										   self.formData,@"formData", nil];
		
		if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
						   parameters:parameters 
					   confirmMessage:[button objectForKey:@"cfmi"] 
					   naviController:self.navigationController];
		}else{
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
						   parameters:parameters 
					   confirmMessage:nil 
					   naviController:self.navigationController];
		}
		[parameters release];
	}	
}

#pragma mark ---- 事件处理方法 ----
- (void) selectAction: (id) sender {
	[[self findFirstResponder] resignFirstResponder];
	if ([DateTimeSelectView instance].isShown) {
		[[DateTimeSelectView instance] selectCancelAction];
	}
	if ([LookupSelectView instance].isShown) {
		[[LookupSelectView instance] selectCancelAction];
	}
	if ([editButtons count] == 1) {
		NSMutableDictionary *button = [editButtons objectAtIndex:0];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		bool pageCheck = NO;
		if ([button objectForKey:@"pck"]) {
			pageCheck = [[button objectForKey:@"pck"]boolValue];
		}
		[self setDataBeforeCommit];
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		if (pageCheck) {
			if ([self isFormVaild]) {
				NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
												   buttonId,@"buttonId",
												   @"edit_form",@"buttonType",
												   self.dataObjectName,@"dataObjectName",
												   self.formData,@"formData", nil];
				if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
					[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
								   parameters:parameters 
							   confirmMessage:[button objectForKey:@"cfmi"] 
							   naviController:self.navigationController];
				}else{
					[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
								   parameters:parameters 
							   confirmMessage:nil 
							   naviController:self.navigationController];
				}
				[parameters release];
			}
		}else {
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"edit_form",@"buttonType",
											   self.dataObjectName,@"dataObjectName",
											   self.formData,@"formData", nil];
			
			if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:[button objectForKey:@"cfmi"] 
						   naviController:self.navigationController];
			}else{
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:nil 
						   naviController:self.navigationController];
			}
			[parameters release];
		}
		return;
	}
	
	//导航bar上面的操作按钮弹出菜单
	UIActionSheet *menu = [MCUIToolkit actionSheet:editButtons
										   withTag:2
									  withDelegate:self
										 withTitle:@"选择操作"];
	[menu showFromTabBar:(UITabBar *)[self.navigationController view]];
}

- (void) selectCancelAction: (id) sender {
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)navigationBarButtonActionSheetEvent:(NSInteger)buttonIndex
{
	if (buttonIndex == [editButtons count]) {
		// 按下取消按钮
		return;
	}
	NSMutableDictionary *button = [editButtons objectAtIndex:buttonIndex];
	if ([MCErrorManagement showAuthMessage:button]) {
		return;
	}
	bool pageCheck = NO;
	if ([button objectForKey:@"pck"] != nil) {
		pageCheck = [[button objectForKey:@"pck"]boolValue];
	}
	[self setDataBeforeCommit];
	NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
	if (pageCheck) {
		if ([self isFormVaild]) {
			NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
											   buttonId,@"buttonId",
											   @"edit_form",@"buttonType",
											   self.dataObjectName,@"dataObjectName",
											   self.formData,@"formData", nil];
			
			if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:[button objectForKey:@"cfmi"] 
						   naviController:self.navigationController];
			}else{
				[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
							   parameters:parameters 
						   confirmMessage:nil 
						   naviController:self.navigationController];
			}
			[parameters release];
		}
	}else {
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"edit_form",@"buttonType",
										   self.dataObjectName,@"dataObjectName",
										   self.formData,@"formData", nil];
		
		if ([button objectForKey:@"cfmi"] != nil && ![[button objectForKey:@"cfmi"] isEqualToString:@""]){
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
						   parameters:parameters 
					   confirmMessage:[button objectForKey:@"cfmi"] 
					   naviController:self.navigationController];
		}else{
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] 
						   parameters:parameters 
					   confirmMessage:nil 
					   naviController:self.navigationController];
		}
		[parameters release];
	}
}

- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (actionSheet.tag == 1) {
		[self selectFilePathActionSheetEvent:buttonIndex actionSheet:actionSheet];
	}
	
	if (actionSheet.tag == 2) {
		[self navigationBarButtonActionSheetEvent:buttonIndex];
	}
}

-(void)changeSection:(id)sender {
	tabIndex = [sender selectedSegmentIndex];
	[self.tableview reloadData];
}

#pragma mark ---- 类内部处理方法 ----
- (Boolean) isFormVaild {
	NSString *requiredFields = @"";
	for (NSMutableDictionary *section in [formLayoutCopy objectForKey:@"stn"]) {
		for (NSMutableDictionary *appItem in [section objectForKey:@"item"]) {
			if ([[appItem objectForKey:@"rqd"] boolValue]) {
				
				NSString *name = [MCUIToolkit getFieldName:appItem];
				NSString *fieldType = [MCUIToolkit getFieldType:appItem];
				NSMutableDictionary *fieldData = [[self.formData objectForKey:@"data"] objectForKey:name];
				if ([fieldType isEqualToString:@"S"] || [fieldType isEqualToString:@"J"] || [fieldType isEqualToString:@"Y"]) {
					NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
					if (fileInfos != nil && [fileInfos count] > 0) {
						NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:0];
						if ([fileInfo objectForKey:@"fname"] == nil || [[fileInfo objectForKey:@"fname"] isEqualToString:@""]) {
							requiredFields = [[requiredFields stringByAppendingString:[MCUIToolkit getFieldLabel:appItem]] stringByAppendingString:@","];
						}
					}
				}else {
					if ([fieldData objectForKey:@"value"] == nil || [[fieldData objectForKey:@"value"] isEqualToString:@""]) {
						requiredFields = [[requiredFields stringByAppendingString:[MCUIToolkit getFieldLabel:appItem]] stringByAppendingString:@","];
					}
				}
			}
		}
	}
	if ([requiredFields length] > 0) {
		[self messageBox:@"输入提示" 
				 message:[NSString stringWithFormat:@"下列域必须输入:\n%@",[requiredFields substringToIndex:[requiredFields length]-1]]
										   delegate:self
								  cancelButtonTitle:nil
								  otherButtonTitles:@"确定"];
		return NO;
	}
	return YES;
}

- (void) setLookupBackFillByFieldName: (NSString *)fieldName fieldDataLookupBack:(NSMutableDictionary *)fieldDataLookupBack{	
	NSMutableDictionary *datas = [formData objectForKey:@"data"];
	NSMutableArray *sections = [formLayoutCopy objectForKey:@"stn"];
	if (sections != nil) {
		for(int i = 0; i < [sections count]; i++){
			NSMutableDictionary *section = [sections objectAtIndex:i];
			NSMutableArray *appItems = [section objectForKey:@"item"];
			for(int j = 0; j < [appItems count]; j++){
				NSMutableDictionary *appItem = [appItems objectAtIndex:j];
				NSString *name = [MCUIToolkit getFieldName:appItem];			
				NSString *fieldType = [MCUIToolkit getFieldType:appItem];
				
				if ([fieldName isEqualToString:name]) {
					if (![fieldDataLookupBack objectForKey:@"lbl"] || [[fieldDataLookupBack objectForKey:@"lbl"] isEqualToString:@""]) {
						if([fieldType isEqualToString: @"3"] || [fieldType isEqualToString: @"4"] || [fieldType isEqualToString: @"U"]){
							NSString *relationObject = [MCUIToolkit getRelationObject:appItem];
							if(relationObject != nil && ![relationObject isEqualToString:@""]){
								id relReturnObject = [[MServerDAO sharedInstance] getFormData:relationObject layoutName:@"" key:[fieldDataLookupBack objectForKey:@"value"] isEditable:@"1"];
								if(relReturnObject){
									NSMutableDictionary *relObject = (NSMutableDictionary *)relReturnObject;
									[fieldDataLookupBack setObject:[relObject objectForKey:@"desc"] == nil?@"":[relObject objectForKey:@"desc"] forKey:@"lbl"];
								}
							}
						}
					}
					[datas setObject:fieldDataLookupBack forKey:name];
				}
			}
		}
	}
	[self.tableview reloadData];
}

- (void) setDataBeforeCommit {
	@try {
		for (NSMutableDictionary *section in [formLayoutCopy objectForKey:@"stn"]) {
			for (NSMutableDictionary *appItem in [section objectForKey:@"item"]) {
				NSString *name = [MCUIToolkit getFieldName:appItem];			
				NSString *fieldType = [MCUIToolkit getFieldType:appItem];
				
				NSMutableDictionary *fieldData = [[self.formData objectForKey:@"data"] objectForKey:name];
				if ([fieldType isEqualToString:@"Y"]) {
					NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
					if (fileInfos != nil) {
						if ([fileInfos count] == 0) {
							[fieldData removeObjectForKey:@"fi"];
						}else {
							for(int i = 0; i < [fileInfos count]; i++){
								NSMutableDictionary *fileInfo = [fileInfos objectAtIndex:i];
								if ([fileInfo objectForKey:@"state"]) {
									NSString *fileState = [fileInfo objectForKey:@"state"];
									if ([fileState isEqualToString:@"add"]) {
										[fileInfo setObject:@"A" forKey:@"opt"];
										[fileInfo removeObjectForKey:@"state"];
									}else if ([fileState isEqualToString:@"normal"]) {
										[fileInfo removeObjectForKey:@"state"];
									}else if ([fileState isEqualToString:@"del"]) {
										[fileInfo setObject:@"" forKey:@"fname"];
										[fileInfo setObject:@"D" forKey:@"opt"];
										[fileInfo removeObjectForKey:@"state"];
									}else if ([fileState isEqualToString:@"addDel"]) {
										[fileInfos removeObjectAtIndex:i];
										i--;
									}
								}
							}
						}	
					}
				} else if ([fieldType isEqualToString:@"J"] || [fieldType isEqualToString:@"S"]) {
					NSMutableArray *fileInfos = [fieldData objectForKey:@"fi"];
					if (fileInfos != nil) {
						if ([fileInfos count] == 1) {
							if (![[fileInfos objectAtIndex:0] objectForKey:@"fname"] || ![[fileInfos objectAtIndex:0] objectForKey:@"lcn"]) {
								[fieldData removeObjectForKey:@"fi"];
							}	
						}
					}
				}
			}
		}
	}
	@catch (NSException * e) {
		[self getServerError:@"" returnMessage:[e reason]];
	}
}

- (void)doCalcAction:(MCTextField *)valueField {
	if (valueField.formulaStyle) {
		NSMutableDictionary* formulaStyle = valueField.formulaStyle;
		NSString *lookupBack = [formulaStyle objectForKey:@"lookup"];
		NSString *seviceDefId = [formulaStyle objectForKey:@"action"];
		if (!lookupBack) {
			return;
		}
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   seviceDefId,@"sid",
										   lookupBack,@"lookupBack",
										   self.dataObjectName,@"dataObjectName",
										   self.formData,@"formData", nil];
		
		[MCFunction pushViewByUrl:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"calc", @"actionType", nil]
					   parameters:parameters 
				   confirmMessage:nil 
				   naviController:self.navigationController];
		
		[parameters release];
	}	
}

//- (void)setFormulaData:(NSString *)fieldName fieldData:(NSMutableDictionary *)fieldData {
//	[[formData objectForKey:@"data"] setObject:fieldData forKey:fieldName];
//	[self.tableview reloadData];
//}
- (void)setFormulaData:(NSMutableDictionary *)returnValue {
	if ([returnValue objectForKey:@"name"]&&[returnValue objectForKey:@"value"]) {
		NSArray *names = [[returnValue objectForKey:@"name"] componentsSeparatedByString:@";"];
		NSArray *values = [[returnValue objectForKey:@"value"] componentsSeparatedByString:@";"];
		for(int i=0; i < [names count]; i++) {
			NSMutableDictionary *fieldData = [NSMutableDictionary dictionary];
			[fieldData setObject:[values objectAtIndex:i] forKey:@"value"];
			[[formData objectForKey:@"data"] setObject:fieldData forKey:[names objectAtIndex:i]];
		}
	}
	[self.tableview reloadData];
}

- (void)autoBackFill:(NSString *)fieldName fieldValue:(NSString *)value 
{
	NSMutableDictionary *fieldData = [[formData objectForKey:@"data"] objectForKey:fieldName];
	if (fieldData) {
		[fieldData setObject:value == nil?@"":value forKey:@"value"];
		[self.tableview reloadData];
	}
}

#pragma mark 键盘相关处理

- (void)textFieldDidEndEditing:(UITextField *)textField {
	MCTextField *valueField = (MCTextField*)textField;
	NSString* fieldName = valueField.fieldName;
	NSMutableDictionary *fieldData = [[self.formData objectForKey:@"data"] objectForKey:fieldName];
	[fieldData setObject:valueField.text == nil?@"":valueField.text forKey:@"value"];
	[self doCalcAction:valueField];
}

#pragma mark ---处理接收到的消息---
//- (void) getImageSize:(id)anObject
- (void)getImageSize:(NSNotification *)notification
{
	MCSize* size = [notification object];
	
	[imageSizeMap setObject:size forKey:size.filename];
	[size release];
	[self.tableview reloadData];
}
#pragma mark-----------代码号：0048  代码名：百度地图   2013-06-13 16:54:53-----------
- (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error
{
//    NSLog(@"调试号:%d",error);
    if (error != 0) {
        //        self.position1.text = @"未知地理位置";
        //        return;
    }else{
        //        // 在此处添加您对反地理编码结果的处理结果
        //        NSString *accurateSite = result.strAddr;
        //        NSLog(@"百度地址信息:%@",result.strAddr);
        ////        self.position1.text = accurateSite;
        //        if ((accurateSite == nil) || [accurateSite isEqualToString:@""]) {
        //            self.position1.text = @"未知地理位置";
        //        }else{
        //            self.position1.text = accurateSite;
        //        }
    }
}

- (void)mapView:(BMKMapView *)mapView didUpdateUserLocation:(BMKUserLocation *)userLocation
{
	if (userLocation != nil) {
        //        NSLog(@"百度的坐标位置:%f %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);
//        CLLocation *baiduLoc = [[CLLocation alloc] initWithLatitude:userLocation.location.coordinate.latitude longitude:userLocation.location.coordinate.longitude];//7月6日修改
//        [baiduLoc release];
        _coordinate.latitude = userLocation.location.coordinate.latitude;
        _coordinate.longitude = userLocation.location.coordinate.longitude;
        
#pragma mark--------代码号：0051 代码名：百度逆向解析开始    2013-05-24 16:44:44
        self.jingField.text = [NSString stringWithFormat:@"%f",_coordinate.longitude];
        
        self.weiField.text = [NSString stringWithFormat:@"%f",_coordinate.latitude];
        
         NSLog(@"_coordinate.latitude=%f,%f",_coordinate.latitude,_coordinate.longitude);
        
        [self.jingField becomeFirstResponder];
        [self.weiField becomeFirstResponder];
        [self performSelector:@selector(lockLocationField) withObject:nil afterDelay:2.0];
        
        //发起反地理编码
        //        [self._search2 reverseGeocode:userLocation.location.coordinate];
        //        停止定位
        [self.mapView3 setShowsUserLocation:NO];
        //    end
    }
    
	
}

@end
